import csv
import datetime
import os
import re
import sys
import requests
from pub_func import DownloadFile, folder_exist, get_utc_time
import datetime
import time

cwd = os.getcwd()
sys.path.append(cwd)

def img_1_24_url(day_str,hour_str):
    img_1_24_url_list = []
    img_1_24_url_list.append('/fdpverifygrid/wind/RMAPS-ST/RMAPS-ST_1KM_BTH_10UV_' + day_str +'_02400060_'+ hour_str + '_BTH_uv10m_bias.png')        #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/CYF/CYF_1KM_BTH_10UV_' + day_str +'_04800060_'+ hour_str + '_BTH_uv10m_bias.png')                  #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/IAPSD3/IAPSD3_1KM_BTH_10UV_' + day_str +'_07200060_'+ hour_str + '_BTH_uv10m_bias.png')            #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/MOML/MOML_1KM_BTH_10UV_' + day_str +'_24000060_'+ hour_str + '_BTH_uv10m_bias.png')                #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/GZ-500m/GZ-500m_1KM_BTH_10UV_' + day_str +'_02400060_'+ hour_str + '_BTH_uv10m_bias.png')          #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/AIW-PMKG/AIW-PMKG_1KM_BTH_10UV_' + day_str +'_02400060_'+ hour_str + '_BTH_uv10m_bias.png')        #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/CAMS-SAFES/CAMS-SAFES_1KM_BTH_10UV_' + day_str +'_03600060_'+ hour_str + '_BTH_uv10m_bias.png')    #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/GRAPES/GRAPES_1KM_BTH_10UV_' + day_str +'_02400060_'+ hour_str + '_BTH_uv10m_bias.png')            #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/KJAINWP/KJAINWP_1KM_BTH_10UV_' + day_str +'_04800060_'+ hour_str + '_BTH_uv10m_bias.png')          #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/YUFENG/YUFENG_100M_YQQ_10UV_' + day_str +'_02400060_'+ hour_str + '_YQQ_uv10m_bias.png')           #1-24
    img_1_24_url_list.append('/fdpverifygrid/wind/MOJI-AIHRWF/MOJI-AIHRWF_1KM_BTH_10UV_' + day_str +'_02400060_'+ hour_str + '_BTH_uv10m_bias.png')  #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/RMAPS-ST/RMAPS-ST_1KM_BTH_2T_' + day_str + '_02400060_' + hour_str + '_BTH_t2m_bias.png')      #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/CYF/CYF_1KM_BTH_2T_' + day_str + '_04800060_' + hour_str + '_BTH_t2m_bias.png')                #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/IAPSD3/IAPSD3_1KM_BTH_2T_' + day_str + '_07200060_' + hour_str + '_BTH_t2m_bias.png')          #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/HWAI/HWAI_1KM_BTH_2T_' + day_str + '_04800060_' + hour_str + '_BTH_t2m_bias.png')              #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/MOML/MOML_1KM_BTH_2T_' + day_str + '_24000060_' + hour_str + '_BTH_t2m_bias.png')              #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/GZ-500m/GZ-500m_1KM_BTH_2T_' + day_str + '_02400060_' + hour_str + '_BTH_t2m_bias.png')        #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/AIW-PMKG/AIW-PMKG_1KM_BTH_2T_' + day_str + '_02400060_' + hour_str + '_BTH_t2m_bias.png')      #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/CAMS-SAFES/CAMS-SAFES_1KM_BTH_2T_' + day_str + '_03600060_' + hour_str + '_BTH_t2m_bias.png')  #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/GRAPES/GRAPES_1KM_BTH_2T_' + day_str + '_02400060_' + hour_str + '_BTH_t2m_bias.png')          #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/KJAINWP/KJAINWP_1KM_BTH_2T_' + day_str + '_04800060_' + hour_str + '_BTH_t2m_bias.png')        #1-24
    img_1_24_url_list.append('/fdpverifygrid/tem/MOJI-AIHRWF/MOJI-AIHRWF_1KM_BTH_2T_' + day_str + '_02400060_' + hour_str + '_BTH_t2m_bias.png')        #1-24
    return img_1_24_url_list

def img_0_24_url(day_str,hour_str):
    img_0_24_list = []
    img_0_24_list.append('/fdpverifygrid/wind/WARMS/WARMS_1KM_BTH_10UV_' + day_str + '_02400060_' + hour_str +'_BTH_uv10m_bias.png')           #0-24
    img_0_24_list.append('/fdpverifygrid/wind/GRAPES-BLD/GRAPES-BLD_1KM_BTH_10UV_' + day_str + '_03600060_' + hour_str +'_BTH_uv10m_bias.png') #0-24
    img_0_24_list.append('/fdpverifygrid/tem/WARMS/WARMS_1KM_BTH_2T_' + day_str + '_02400060_' + hour_str +'_BTH_t2m_bias.png')                #0-24
    img_0_24_list.append('/fdpverifygrid/tem/GRAPES-BLD/GRAPES-BLD_1KM_BTH_2T_' + day_str + '_03600060_' + hour_str +'_BTH_t2m_bias.png')      #0-24
    img_0_24_list.append('/fdpverifygrid/wind/NMCGRID-GMOSRR/NMCGRID-GMOSRR_1KM_OMA_10UV_' + day_str + '_02400060_' + hour_str +'_OMA_uv10m_bias.png')      #0-24
    return img_0_24_list

def img_0_12_url(day_str,hour_str):
    img_0_12_list = []
    img_0_12_list.append('/fdpverifygrid/wind/RMAPS-RISE/RMAPS-RISE_1KM_BTH_10UV_'+ day_str +'_01200060_' + hour_str + '_BTH_uv10m_bias.png')               #0-12
    img_0_12_list.append('/fdpverifygrid/tem/RMAPS-RISE/RMAPS-RISE_1KM_BTH_2T_' + day_str + '_01200060_' + hour_str + '_BTH_t2m_bias.png')                  #0-12
    img_0_12_list.append('/fdpverifygrid/wind/RMAPS-RISE/RMAPS-RISE_100M_OMA_10UV_' + day_str + '_01200060_' + hour_str + '_OMA_uv10m_bias.png')            #0-12
    img_0_12_list.append('/fdpverifygrid/wind/WiNS/WiNS_100M_ZJK_10UV_' + day_str + '_01200010_' + hour_str + '_ZJK_uv10m_bias.png')                        #0-12
    img_0_12_list.append('/fdpverifygrid/tem/RMAPS-RISE/RMAPS-RISE_100M_OMA_2T_' + day_str + '_01200060_' + hour_str + '_OMA_t2m_bias.png')                 #0-12
    img_0_12_list.append('/fdpverifygrid/tem/WiNS/WiNS_100M_ZJK_2T_' + day_str + '_01200010_' + hour_str + '_ZJK_t2m_bias.png')                             #0-12
    return img_0_12_list

def img_0_23_url(day_str,hour_str):
    img_0_23_list = []
    img_0_23_list.append('/fdpverifygrid/tem/NMCGRID-GMOSRR/NMCGRID-GMOSRR_1KM_OMA_2T_' + day_str + '_02400060_' + hour_str + '_OMA_t2m_bias.png')      #1-23
    return img_0_23_list

def img_1_2_url(day_str,hour_str):
    img_1_2_list = []
    img_1_2_list.append('/fdpverifygrid/wind/INCA-HR/INCA-HR_100M_ZJK_10UV_' + day_str + '_00200060_' + hour_str + '_ZJK_uv10m_bias.png')       #1-2
    img_1_2_list.append('/fdpverifygrid/wind/AIDF-WEN/AIDF-WEN_1KM_BTH_10UV_' + day_str + '_00200010_' + hour_str + '_BTH_uv10m_bias.png')      #1-2
    img_1_2_list.append('/fdpverifygrid/tem/AIDF-WEN/AIDF-WEN_1KM_BTH_2T_' + day_str + '_00200010_' + hour_str + '_BTH_t2m_bias.png')           #1-2
    return img_1_2_list

def Download_img(str_day,hour_start,hour_end):
    hour_day_list = []
    hour_day_list.append(str_day + '1800')
    hour_day_list.append(str_day + '0000')
    hour_day_list.append(str_day + '0600')
    hour_day_list.append(str_day + '1200')

    if hour_start == 1:
        list1 = [ '00' + str(i+1)for i in range(-1,int(hour_end))]
    elif hour_start == 0:
        list1 = [ '00' + str(i+1)for i in range(-1,int(hour_end))]
    hour_list = [ num[1:]  if len(num) == 4  else num for num in list1]
    # print(day_list)
    # print(hour_list)
    if hour_start == 1 and hour_end == 24:
        for hour_day in hour_day_list:
            for hour_str in hour_list:
                Download_list = img_1_24_url(str(hour_day),str(hour_str))
                # print(Download_list)
                download(Download_list)
    elif hour_start == 0 and hour_end == 24:
        for hour_day in hour_day_list:
            for hour_str in hour_list:
                Download_list = img_0_24_url(str(hour_day),str(hour_str))
                download(Download_list)
    elif hour_start == 0 and hour_end == 12:
        for hour_day in hour_day_list:
            for hour_str in hour_list:
                Download_list = img_0_12_url(str(hour_day),str(hour_str))
                download(Download_list)
    elif hour_start == 0 and hour_end == 23:
        for hour_day in hour_day_list:
            for hour_str in hour_list:
                Download_list = img_0_23_url(str(hour_day),str(hour_str))
                download(Download_list)
    elif hour_start == 1 and hour_end == 2:
        for hour_day in hour_day_list:
            for hour_str in hour_list:
                Download_list = img_1_2_url(str(hour_day),str(hour_str))
                download(Download_list)

def download(Download_list):
    url = 'http://smart2022-fdp.ium.cn'
    for img_url in Download_list:
        download_url = url + img_url
        # print(download_url)
        img_name = download_url.split('/')[-1]
        folder_name = download_url.split('/')[-2]
        day_name = download_url.split('_')[-6]
        file_type = download_url.split('/')[-3]
        path = '/media/ubuntu/08f29263-3882-43db-96fd-febb6f2e2e5f1/20210129/' + file_type + '/' + folder_name + '/'+ day_name+ '/'
        # print(file_type,folder_name,day_name)
        folder_exist(path)
        if os.path.exists(path + img_name) == False:
            DownloadFile(download_url,path,img_name)

if __name__ == '__main__':
    today = datetime.datetime.today()
    str_day = today + datetime.timedelta(days=-2)
    str_day = datetime.datetime.strftime(str_day, "%Y%m%d")
    # str_day = '20210313'
    Download_img(str_day,1,24)
    Download_img(str_day,0,24)
    Download_img(str_day,0,12)
    Download_img(str_day,0,23)
    Download_img(str_day,1,2)







